package org.soderiakelvy.model.dao.imp;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;
import org.soderiakelvy.model.dao.PersonalDao;
import org.soderiakelvy.model.domain.PersonalVO;
import org.soderiakelvy.model.domain.RolVO;
import org.soderiakelvy.model.util.HibernateUtil;

public class PersonalDaoImp implements PersonalDao,Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void insertPersonalVO(PersonalVO personal) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(personal);
        session.getTransaction().commit();
        session.close();
	}

	@Override
	public void updatePersonalVO(PersonalVO personal) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.update(personal);
        session.getTransaction().commit();
        session.close();
	}

	@Override
	public void deletePersonalVO(PersonalVO personal) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.delete(personal);
        session.getTransaction().commit();
        session.close();
	}

	@Override
	public List<PersonalVO> getAllPersonales() {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(PersonalVO.class);
        List<PersonalVO> result = criteria.list();
        session.close();
		return result;
	}

	@Override
	public List<PersonalVO> getPersonalByCategoria(String categoria) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(PersonalVO.class);
        criteria.add(Restrictions.eq("categoria", categoria));
        List<PersonalVO> result = criteria.list();
        session.close();
		return result;
	}

	@Override
	public PersonalVO getPersonalByPersonal(PersonalVO personal) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		String sql = "select p from PersonalVO p where usuario=:user and password=:pass";
		Query query = session.createQuery(sql);
		query.setString("user", personal.getUsuario());
		query.setString("pass", personal.getPassword());
		PersonalVO user =  (PersonalVO) query.uniqueResult();
		session.close();
		return user;
	}

	@Override
	public List<PersonalVO> getPersonalByRol(RolVO rol) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(PersonalVO.class);
        criteria.add(Restrictions.eq("rol", rol));
        List<PersonalVO> result = criteria.list();
        session.close();
		return result;
	}

}
